/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights
 * reserved. <a>http://www.cqcis.com</a>
 */
package com.cqcis.uip.base.framework.core.cluster;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import com.cqcis.uip.base.framework.UIPTaskException;
import com.cqcis.uip.base.framework.common.Task;
import com.cqcis.uip.base.framework.core.Endpoint;
import com.cqcis.uip.base.framework.utils.EndpointGenerator;
import com.cqcis.uip.base.framework.utils.StringUtil;

/**
 * 命令集群支持
 * 
 * @author huangmy
 * @date   2009-1-19
 */
public class ClusterCommandSupport {
	public final static long ENDPOINT_ID = -222; // 命令支持 ID
	private static String CLUSTER_SUPPORT_ARG = "-c";
	private static String ARG_PREFIX = "-";
	private String cmdline; // CMD
	
	public ClusterCommandSupport(String cmdline) {
		if (!StringUtil.hasText(cmdline)) {
			throw new IllegalArgumentException("cmdline Can't be null.");
		}
		this.cmdline = cmdline;
	}
	
	/**
	 * 返回集群命令.
	 * 
	 * @return null 或者 length为0 不支持
	 */
	public String[] getClusterArgs() {
		CS cs = hasCluster();
		if (cs != null && cs.support) {
			return cs.args;
		}
		
		return new String[0];
	}
	
	/**
	 * 是否集群
	 * 
	 * @return
	 */
	public boolean isCluster() {
		CS cs = hasCluster();
		if (cs != null) {
			return cs.support;
		}
		
		return false;
	}
	
	/**
	 * Command support task
	 * 
	 * @return
	 */
	public static Task createCommandTask(String nodeZName) {
		Task task = new Task(ClusterCommandSupport.class.getName());
		
		Map<String, String> props = new HashMap<String, String>();
		props.put(EndpointGenerator.ENDPOINT_EXPRESSION_LOCATION, nodeZName);
		Endpoint to = EndpointGenerator.generate(ENDPOINT_ID, props);
		
		task.route().to(to);
		task.route().from(EndpointGenerator.generate(ENDPOINT_ID));
		
		return task;
	}
	
	/**
	 * 是否需要集群支持
	 * 
	 */
	private void generateAndCreateCS(String arg, StringTokenizer st, CS cs) {
		arg = StringUtil.trimWhiteToNull(arg);
		if (CLUSTER_SUPPORT_ARG.equalsIgnoreCase(arg)) {
			cs.support = true;
			
			boolean asArg = true;
			List<String> appArgs = new ArrayList<String>(); 
			while (st.hasMoreTokens()) {
				String app = st.nextToken();
				app = StringUtil.trimWhiteToNull(app);
				if (app == null) {
					continue;
				}

				if (CLUSTER_SUPPORT_ARG.equalsIgnoreCase(app)) {
					throw new UIPTaskException("argument format error. Can't with '" 
							+ CLUSTER_SUPPORT_ARG
							+ "' more than one.");
				}
				
				if (app.startsWith(ARG_PREFIX)) {
					asArg = false;
				}
				
				if (app != null && asArg) {
					appArgs.add(app);
				}
			}
			
			cs.args = appArgs.size() > 0 ? appArgs.toArray(new String[appArgs.size()])
					: new String[0];
		}
	}
	
	/**
	 * 集群支持
	 * 
	 * @return
	 */
	private CS hasCluster() {
		CS cs = new CS();
		StringTokenizer st = new StringTokenizer(this.cmdline);
		while (st.hasMoreTokens()) {
			String arg = st.nextToken();
			generateAndCreateCS(arg, st, cs);
		}
		
		if (cs.support && cs.args == null) {
			cs.args = new String[1];
		}
		
		return cs;
	}
	
	class CS {
		boolean support;
		String[] args;		
	}
}
